import pymysql

import settings

from loguru import logger


class MySQLClient:
    def __init__(self):
        self.conn = pymysql.connect(**settings.DB_info)

    def execute(self, sql):
        try:
            with self.conn.cursor() as cursor:
                cursor.execute(sql)
                self.conn.commit()
        except Exception as e:
            logger.error(f"报错：{e}，sql：{sql}")

    def sea(self, sql):
        """这是一个查询SQL语句函数"""
        try:
            with self.conn.cursor() as cursor:
                cursor.execute(sql)
                return cursor.fetchall()
        except Exception as e:
            logger.error(f"报错：{e}，sql：{sql}")

    def close(self):
        self.conn.close()
